package com.xbts.web.controller.system;

import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.core.page.TableDataInfo;
import com.xbts.common.enums.BusinessType;
import com.xbts.common.utils.poi.ExcelUtil;
import com.xbts.system.domain.SysMessage;
import com.xbts.system.service.ISysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 站内消息 Controller
 *
 * @author anhe
 * @date 2022-12-26
 */
@RestController
@RequestMapping("/system/message")
public class SysMessageController extends BaseController {
    @Autowired
    private ISysMessageService sysMessageService;

    /**
     * 查询站内消息 列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SysMessage sysMessage) {
        startPage(sysMessage);
        List<SysMessage> list = sysMessageService.selectSysMessageList(sysMessage);
        return getDataTable(list);
    }

    /**
     * 导出站内消息 列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:export')")
    @Log(title = "站内消息 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody SysMessage sysMessage) {
        List<SysMessage> list = sysMessageService.selectSysMessageList(sysMessage);
        ExcelUtil<SysMessage> util = new ExcelUtil<SysMessage>(SysMessage.class);
        util.exportExcel(response, list, "站内消息 数据");
    }

    /**
     * 获取站内消息 详细信息
     */
    @GetMapping(value = "/getUnreadNum")
    public AjaxResult getUnreadNum() {
        return AjaxResult.success(sysMessageService.getUnreadNum());
    }

    /**
     * 获取用户未读消息数量
     */
    @PreAuthorize("@ss.hasPermi('system:message:query')")
    @PostMapping(value = "/getInfo")
    public AjaxResult getInfo(@RequestBody SysMessage sysMessage) {
        return AjaxResult.success(sysMessageService.selectSysMessageByNoticeId(sysMessage.getNoticeId()));
    }

    /**
     * 新增站内消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @Log(title = "站内消息 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMessage sysMessage) {
        return toAjax(sysMessageService.insertSysMessage(sysMessage));
    }

    /**
     * 新增站内消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @Log(title = "站内消息 ", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addSysMessage(@RequestBody SysMessage sysMessage) {
        return toAjax(sysMessageService.addSysMessage(sysMessage));
    }

    /**
     * 修改站内消息,已读和全部已读
     */
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @Log(title = "站内消息 ", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SysMessage sysMessage) {
        return toAjax(sysMessageService.updateSysMessage(sysMessage));
    }

    /**
     * 删除站内消息
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @Log(title = "站内消息 ", businessType = BusinessType.DELETE)
    @PostMapping ("/remove")
    public AjaxResult remove(@RequestBody List<Long> noticeIds) {
        return toAjax(sysMessageService.deleteSysMessageByNoticeIds(noticeIds));
    }
}
